<template>
  <div>
    <el-dialog title="新增" :visible.sync="dialogVisible" width="30%">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="报告名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请填写报告名称" />
        </el-form-item>
        <el-form-item label="站点Id" prop="station_id">
          <el-select v-model="ruleForm.station_id" placeholder="请选择活动区域">
            <el-option
              v-for="item in allStation"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报告类型" prop="type">
          <el-input v-model="ruleForm.type" placeholder="请填写报告类型" />
        </el-form-item>
        <el-form-item label="报告时间" prop="report_at">
          <el-date-picker
            v-model="ruleForm.report_at"
            format="yyyy-MM"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { allStation, addReport } from '@/api/report'

export default {
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        name: '',
        station_id: '',
        type: '',
        report_at: '',
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        station_id: [
          { required: true, message: '请选择站点', trigger: 'blur' },
        ],
        type: [{ required: true, message: '请输入活动类型', trigger: 'blur' }],
        report_at: [{ required: true, message: '请选择时间', trigger: 'blur' }],
      },
      allStation: [],
    }
  },
  created() {
    this.getStaion()
  },
  methods: {
    open() {
      this.dialogVisible = true
    },
    getStaion() {
      allStation().then((res) => {
        this.allStation = res.data.data
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.report_at = this.dateFormat(this.ruleForm.report_at)
          addReport(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$emit('refresh')
            }
          })
          this.dialogVisible = false

          console.log('this.ruleForm', this.ruleForm)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    dateIfAddZero(time) {
      return time < 10 ? '0' + time : time
    },
    dateFormat(row) {
      var t = new Date(row) // row 表示一行数据, createTime 表示要格式化的字段名称
      if (!t) {
        return ''
      }
      const year = t.getFullYear()
      const month = this.dateIfAddZero(t.getMonth() + 1)
      return year + '-' + month
    },
  },
}
</script>
<style lang="scss" scoped>
</style>
